from pygraph.classes.graph import graph
from pygraph.classes.digraph import digraph
from pygraph.algorithms.searching import breadth_first_search
from pygraph.readwrite.dot import write
import pickle
from pygraph.algorithms.minmax import shortest_path
import sys

def count_inverse_map(themap):
    countmap={}
    #must be a map of int:[list]
    thekeys=themap.keys()
    thekeys.sort()
    for i in thekeys:
        print themap[i]
        print "%d:%d"%(i,len(themap[i]))

    countmap[i]=len(themap[i])
        
def get_inverse_map(themap):
    dep={}
    
    for ikey in themap.keys():
        mydep=themap[ikey]
        if not mydep in dep.keys():
            dep[mydep]=[]
        dep[mydep].append(ikey)
    
    return dep

infname=sys.argv[1]

inf=open(infname,"r")
gr=pickle.load(inf)
inf.close()

starturl="http://www.nersc.gov/nusers/"

nneighbor={}
for inode in gr.nodes():
    nneighbor[inode]=len(gr.neighbors(inode))

inneighbor=get_inverse_map(nneighbor)

(stree,distance)=shortest_path(gr,starturl)

idistance=get_inverse_map(distance)

count_inverse_map(idistance)
count_inverse_map(inneighbor)
